// pages/EditMember/EditMember.js
//var provices = require("../../js/city.js");
var request = require("../../utils/request.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "picture": "../../images/photo.png",
    "id": "",
    'isMine': false,//用来判断是否是当前账号的主成员
    "address": "",
    "birth": "",
    "city": "",
    "authorization": '1',
    "jiancha": "",
    "mobile": "",
    "name": "",
    "province": "",
    "dateType": "create",
    "location": ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.toastDialog = this.selectComponent("#toastDialog");//此处id和页面的 wxml中的id一致
    this.setData({
      dateType: options.dateType
    })

    if (options.dateType == "update" || options.dateType == "mine") {
      this.userInfo(options.id)
    }
    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
  },
/**
 * 用户点击右上角分享
 */
onShareAppMessage (res) {
  return {
    title: '迪佳极视',
    path: 'pages/index/index',
    // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
    success: function (shareTickets) {
      console.info(shareTickets + '成功');
      // 转发成功
    },
    fail: function (res) {
      console.log(res + '失败');
      // 转发失败
    },
    complete:function(res){
      // 不管成功失败都会执行
    }
  }
},
  //成员详情
  userInfo: function (e) {
    let that = this
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/userInfo', {
      "uid": e
    }, function (res) {
      // success
      console.log(res)
      that.toastDialog.hideDialog(); //关闭loading提示框
      let picture = ''
      if (res.info.photo == '') {
        picture = "../../images/default-photo.png"
      } else {
        picture = res.info.photo
      }
      console.log(picture)
      that.setData({
        "id": res.info.id,
        "address": res.info.address,
        "birth": res.info.birth,
        "city": res.info.city,
        "jiancha": res.info.jiancha,
        "mobile": res.info.mobile,
        "name": res.info.name,
        "province": res.info.province,
        "location": res.info.location,
        "authorization": res.info.authorization,
        "picture": picture
      })
      that.mineList()

    }, function (res) {
      // error
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //我的信息
  mineList: function () {
    var that = this;
    // that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框
    request.postData('xcx/myUserInfo', {}, function (res) {
      // that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        console.log(res.userInfo.id);
        console.log(that.data.id);
        if (res.userInfo.id == that.data.id) {
          that.setData({
            isMine: true
          })
        } else {
          that.setData({
            isMine: false
          })
        }
      } else {
        that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
      }
    }, function (res) {
      that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
    });
  },
  //选择日期
  bindTimeChange: function (e) {

    this.setData({
      birth: e.detail.value
    })
  },
  //选择头像
  getAuthorize: function () {
    console.log(this.data.isMine)
    var that = this;
    //等后台给个接口 判断是否设置过昵称和头像，是且isMine=true 调起昵称授权弹窗 否则直接调取相册
    if (that.data.authorization == '0' && that.data.isMine == true) {
      wx.getUserProfile({
        desc: '登录迪佳极视', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log(res)
          that.setData({
            "name": res.userInfo.nickName,
            "picture": res.userInfo.avatarUrl,
            "authorization": '1'
          })
          //等后台给上传头像接口，调用
        },
        fail: function (res) {
          console.log(res)
          console.log('失败')
          that.setData({
            "authorization": '0'
          })
          that.chooseImg()
        }
      })
    } else {
      that.chooseImg()
    }
  },
  chooseImg: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (e) {
        console.log(e);
        that.setData({
          "picture": e.tempFilePaths[0],
        })
        var data = { photo: e.tempFilePaths, uid: that.data.id }
        let url = 'https://wxxcx.di.plus/djeye/xcx/updateUserPhoto'
        if (that.data.dateType == "update" || that.data.dateType == "mine") {
          url = 'https://wxxcx.di.plus/djeye/xcx/updateUserPhoto'
        }else if(that.data.dateType == "create"){
          url = 'https://wxxcx.di.plus/djeye/xcx/uploadPhoto'
        }
        wx.uploadFile({
          url: url,
          filePath: e.tempFilePaths[0],
          header: { "openId": wx.getStorageSync("openid") },
          name: 'photo',
          formData: {
            'uid': that.data.id
          },
          success: function (res) {
            console.log(res)
            var sss = JSON.parse(res.data)
            if (that.data.dateType == "update" || that.data.dateType == "mine") {
              var dizhi = sss.photo;

            }else if(that.data.dateType == "create"){
              dizhi = sss.photoUrl;
            }
            that.setData({
              photo:dizhi,
              picture:dizhi
            })
            //输出图片地址 
            console.log(dizhi);

            //do something 
          }, fail: function (err) {
            console.log(err)
          }
        });
      },
      fail: function (e) {

      }
    })
  },
  getlocation: function () {
    wx.navigateTo({
      url: '../getlocation/getlocation',
    })
  },
  //选择行政区划
  bindRegionChange: function (e) {
    this.setData({
      province: e.detail.value[0],
      city: e.detail.value[1]
    })
  },

  //保存
  formSubmit(e) {
    var that = this
    console.log(that.data.photo)
    if (e.detail.value.name == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请输入姓名',
        timeout: true
      });
      return false
    }
    if (e.detail.value.birth == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请选择出生日期',
        timeout: true
      });
      return false
    }
    if (e.detail.value.location == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请选择所在地区',
        timeout: true
      });
      return false
    }
    if(that.data.photo==undefined){
      e.detail.value.photo=''
    }else{
      e.detail.value.photo = that.data.photo

    }
    console.log(e.detail.value)

    that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框

    var url, headers;
    if (that.data.dateType == "update" || that.data.dateType == "mine") {
      url = "https://wxxcx.di.plus/djeye/xcx/updateMyUser";
      headers = "application/x-www-form-urlencoded"
    } else {
      url = "https://wxxcx.di.plus/djeye/xcx/addMyUser";
      headers = "application/x-www-form-urlencoded"
    }
    if(e.detail.value.picture=='' && that.data.authorization=='0'){
      e.detail.value.authorization='0'
    }else{
      e.detail.value.authorization='1'
    }
    console.log(that.data);

    wx.request({
      //项目的真正接口，通过字符串拼接方式实现
      url: url,
      header: {
        "content-type": headers,
        "openId": wx.getStorageSync("openid")
      },
      data: e.detail.value,
      method: 'POST',
      success: function (res) {
        that.toastDialog.hideDialog(); //关闭loading提示框
        //参数值为res.data,直接将返回的数据传入
        console.log(res)
        if (res.data.code == "1") {
          //console.log("000")
          that.toastDialog.showDialog({ code: res.data.code, message: res.data.message, timeout: true });
          return false
        } else {
          that.toastDialog.showDialog({ code: res.data.code,message:'提交成功', tips: "" ,timeout: true });
          setTimeout(() => {
            if(that.data.dateType=='mine'){
              wx.switchTab({
                url: '../mine/mine'
              });
            }else{
              // wx.navigateTo({
              //   url: '../member/member',
              // })
              wx.navigateBack({
                delta: '1',
              })
            }
          }, 2000);
        }

      },
      fail: function (res) {
        that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
      },
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})